//
//  HomeViewController.m
//  eshow
//
//  Created by Hisoka on 15/11/11.
//  Copyright © 2015年 Hisoka. All rights reserved.
//

#import "HomeViewController.h"
#import "LoginSelectViewController.h"
#import "MyViewController.h"
#import "ResumeTplViewController.h"
#import "sys/utsname.h"
#import "UIBarButtonItem+Badge.h"

@interface HomeViewController ()

@property (nonatomic, strong) UIImageView *bgView;
@property (nonatomic, strong) UIButton *makeBTN;
@property (nonatomic, strong) NSString *isLogin;

@end

@implementation HomeViewController

- (void) viewWillAppear:(BOOL)animated {
    
    [MobClick beginLogPageView:@"Page_Home"];
    
    self.isLogin = [[TMCache sharedCache] objectForKey:@"loginStatus"];
    if (self.navigationItem.rightBarButtonItem != nil) {
        [self getMsgTotal];
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setStyle];
    [self getDeviceInfo];
}

- (void) setStyle {
    self.navigationItem.title = @"首页";
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"我的 "
                                                              style:UIBarButtonItemStyleDone
                                                             target:self
                                                             action:@selector(pushToMyVC)];
    
    self.navigationItem.rightBarButtonItem.tintColor = ORANGE_COLOR;
    
    self.bgView = [UIImageView newAutoLayoutView];
    [self.view addSubview:_bgView];
    
    if ([self isiPadModel:[[TMCache sharedCache] objectForKey:@"deviceModel"]] == YES) {
        [_bgView autoPinEdge:ALEdgeTop toEdge:ALEdgeTop ofView:self.view withOffset:-44];
    }else{
        [_bgView autoPinEdge:ALEdgeTop toEdge:ALEdgeTop ofView:self.view];
    }

    [_bgView autoPinEdge:ALEdgeLeft toEdge:ALEdgeLeft ofView:self.view];
    [_bgView autoPinEdge:ALEdgeBottom toEdge:ALEdgeBottom ofView:self.view];
    [_bgView autoPinEdge:ALEdgeRight toEdge:ALEdgeRight ofView:self.view];

    _bgView.image = [UIImage imageNamed:@"homeBg"];
    
    self.makeBTN = [UIButton newAutoLayoutView];
    [self.view addSubview:_makeBTN];
    
    [_makeBTN autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:self.view withOffset:-130];
    [_makeBTN autoAlignAxis:ALAxisVertical toSameAxisOfView:_bgView];
    [_makeBTN autoSetDimensionsToSize:CGSizeMake(254, 46)];
    [_makeBTN setBackgroundImage:[UIImage imageNamed:@"makeResumeBTN"] forState:UIControlStateNormal];
    [_makeBTN addTarget:self action:@selector(pushToMakeResume) forControlEvents:UIControlEventTouchUpInside];
    
    self.isLogin = [[TMCache sharedCache] objectForKey:@"loginStatus"];

}

- (BOOL) isiPadModel:(NSString *)model {
    
    if ([model rangeOfString:@"iPhone4"].location != NSNotFound) return YES;
    
    if ([model rangeOfString:@"iPad"].location != NSNotFound) return YES;
    
    return NO;
}

#pragma mark - 制作我的简历
- (void) pushToMakeResume {
    
    [MobClick event:@"Click_Home_MakeMyResumeBTN"];
    
    if ([_isLogin isEqualToString:@"1"]) {
        ResumeTplViewController *resumeTplVC = [[ResumeTplViewController alloc] init];
        [self.navigationController pushViewController:resumeTplVC animated:YES];
    }else{
        dispatch_async(dispatch_get_main_queue(), ^{
            [self initLoginVC];
        });
    }
}

#pragma mark 进入我的页面
- (void) pushToMyVC {

    [MobClick event:@"Click_Home_MyBTN"];

    if ([_isLogin isEqualToString:@"1"]) {
        MyViewController *myVC = [[MyViewController alloc] init];
        [self.navigationController pushViewController:myVC animated:YES];
    }else{
        dispatch_async(dispatch_get_main_queue(), ^{
            [self initLoginVC];
        });
    }
    
}

#pragma mark - 实例登录界面

- (void) initLoginVC {
    LoginSelectViewController *loginVC = [[LoginSelectViewController alloc] init];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:loginVC];
    [self.navigationController presentViewController:nav animated:YES completion:nil];
}

#pragma mark - 获取设备型号
- (void) getDeviceInfo {
    
    NSString *cache = [[TMCache sharedCache] objectForKey:@"deviceModel"];
    
    NSLog(@"device model %@", cache);
    
    if (cache != nil) return;
    
    struct utsname systemInfo;
    uname(&systemInfo);
    NSString *deviceModel = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
    
    [[TMCache sharedCache] setObject:deviceModel forKey:@"deviceModel"];
    
     NSLog(@"device model %@", deviceModel);
    
}

#pragma mark - 获取用户新消息总数
- (void) getMsgTotal {
    
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setObject:[NSString stringWithFormat:@"%@", [[TMCache sharedCache] objectForKey:@"SSID"]] forKey:@"ssid"];
    
    [[HttpManager instance] requestWithMethod:@"User/getProfile"
                                   parameters:parameters
                                      success:^(NSDictionary *result) {
                                          
                                          NSString *newMsgTotal = [[result objectForKey:@"data"] objectForKey:@"new_message"];

                                          if ([newMsgTotal isEqualToString:@"0"]) {
                                              self.navigationItem.rightBarButtonItem.badgeValue = nil;
                                          }else{
                                              self.navigationItem.rightBarButtonItem.badgeValue = newMsgTotal;
                                          }

                                      }
                                      failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                                          self.navigationItem.rightBarButtonItem.badgeValue = nil;
//                                          [SVProgressHUD showInfoWithStatus:[error localizedDescription] maskType:SVProgressHUDMaskTypeBlack];
                                      }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
